<?php
namespace app\ice\model;
use think\Db;
use think\Model;

class Order extends Model
{
    protected $table = 'ig_order';

    public function orderGoods()
    {
        return $this->hasMany('OrderGoods','order_id')->field('goods_id,goods_num');;
    }

    public function addParkOrder($param){
        $result = $this->allowField(true)->save($param);
        return $result;
    }
    // 查看订单详情
    public function getOrderDetail($param)
    {
        $data = $this->where('order_id',$param['id'])->where('uid',$param['uid'])->find();
        return $data;
    }

    public function saveOrder($param){
        $data = $this->where('order_id',$param['orderId'])->where('uid',$param['uid'])->update(['status'=>$param['status']]);
        return $data;
    }

    public function getOrder($uid,$status){
        $orderList = $this->where('uid',$uid)->where('status',$status)->order('order_id desc')->select();
        if($orderList){
            foreach ($orderList as $key => &$value) {
                $value['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
                $value['amountReal'] = $value['goods_price'] + $value['shipping_price'];
                $value['score'] = 0;
                switch ($value['status']) {
                    case 0:
                        $value['statusStr'] = '待付款';
                        break;
                    case 1:
                        $value['statusStr'] = '待发货';
                        break;
                    case 2:
                        $value['statusStr'] = '待收货';
                        break;
                    case 3:
                        $value['statusStr'] = '待评价';
                        break;
                    case 4:
                        $value['statusStr'] = '已完成';
                        break;
                }
                $pics = $this->table('ig_order_goods b','a.order_id = b.order_id')->join('ig_goods c','c.id = b.goods_id')->where('b.order_id',$value['order_id'])->column('pic');
                foreach ($pics as $k => $v) {
                    $pics[$k] = config('host').$v;
                }
                $value['pics'] = $pics;
            }
            unset($value);
            unset($key);
        }
        return $orderList;
    }

    public function getPrizeOrder($uid,$page,$orll_page){
        $orderList['row'] = $this->table('ig_order a')
        ->where('uid',$uid)
        ->field('c.pic,c.goods_name,a.add_time,a.status,a.order_id')
        ->where('type',2)
        ->join('ig_order_goods b','a.order_id = b.order_id')
        ->join('ig_goods c','c.id = b.goods_id')
        ->page($page,$orll_page)
        ->order('a.order_id desc')
        ->select();
        $total = $this->table('ig_order a')
        ->where('uid',$uid)
        ->where('type',2)
        ->join('ig_order_goods b','a.order_id = b.order_id')
        ->join('ig_goods c','c.id = b.goods_id')
        ->count();
        $orderList['count'] = ceil($total/$orll_page);
        $orderList['page'] =  $page;
        if(count($orderList['row']) < 1 ){
            return false;
        }
        if($orderList['row']){
            foreach ($orderList['row'] as $key => &$value) {
                $value['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
                $value['pic'] = config('host').$value['pic'];;
            }
            unset($value);
            unset($key);
        }
        return $orderList;
    }

    public function getStatistics($uid){
        return $data = $this->field('sum(status=0) count_id_no_pay,sum(status=1) count_id_no_transfer,sum(status=2) count_id_no_confirm,sum(status=3) count_id_no_reputation,sum(status=4) count_id_success')->where('uid',$uid)->find();
    }

    public function getParkOrderList($param)
    {
        $data = $this->alias('a')->where('uid',$param['uid'])->join('cbdd_park b','a.park_id = b.id')->select();
        foreach ($data as $key => &$v) {
            $v['show_time'] = date('Y-m-d H:i:s',($v['order_time'] + $v['day'] *60*60*24));
            $v['order_time'] = date('Y-m-d H:i:s',$v['order_time']);
        }
        unset($key);
        unset($v);
        return $data;
    }
}